package com.taiji.dianthus.service.sms.impl;

import com.alibaba.fastjson.JSONObject;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.sms.SmsMoInfoRepository;
import com.taiji.dianthus.domain.app.AppInfo;
import com.taiji.dianthus.domain.sms.SmsAccount;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sms.SmsMoInfo;
import com.taiji.dianthus.domain.sms.api.SmsDomain;
import com.taiji.dianthus.service.api.SmsPublicUseService;
import com.taiji.dianthus.service.app.AppInfoService;
import com.taiji.dianthus.service.sms.SmsAccountService;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.service.sms.SmsMoInfoService;
import com.taiji.dianthus.util.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SmsInfoServiceImnpl
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Service
@Transactional
public class SmsMoInfoServiceImpl implements SmsMoInfoService {


    @Autowired
    private SmsMoInfoRepository moInfoRepository;
    @Autowired
    private SmsInfoService smsInfoService;
    @Autowired
    private SmsAccountService accountService;
    @Autowired
    private SmsPublicUseService publicUseService;
    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private RestUtil restUtil;


    @Override
    public List<SmsMoInfo> getBySmsInfoId(String id) {
        List<SmsMoInfo> moInfoList = null;
        try {
            SmsInfo smsInfo = smsInfoService.getOne(id);
            moInfoList = moInfoRepository.getBySmsInfoId(id);
            String[] userPhones = smsInfo.getReceiveUserPhone().split(",");
            if (moInfoList.size() > 0 && moInfoList.size() < userPhones.length) {
                for (int i = 0; i < userPhones.length; i++) {
                    boolean flag = false;
                    for (int j = 0; j < moInfoList.size(); j++) {
                        SmsMoInfo moInfo = moInfoList.get(j);
                        if (moInfo.getMobile().equals(userPhones[i])) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        SmsMoInfo moInfo = new SmsMoInfo();
                        moInfo.setMobile(userPhones[i]);
                        moInfoList.add(moInfo);
                    }
                }
            } else if (moInfoList.size() <= 0) {
                for (int i = 0; i < userPhones.length; i++) {
                    SmsMoInfo moInfo = new SmsMoInfo();
                    moInfo.setMobile(userPhones[i]);
                    moInfoList.add(moInfo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return moInfoList;
    }

    @Override
    public BusinessMessage saveLastInfo(SmsMoInfo moInfo) {
        BusinessMessage message = new BusinessMessage();
        try {
            moInfo.setCreateTime(new Date());
            SmsAccount smsAccount = accountService.getIdByMobile(moInfo.getAddSerial());
            if (smsAccount != null && smsAccount.getIsOutCall() == 1) {
                //外部调用
                SmsDomain smsDomain = publicUseService.getInfoByAccountId(smsAccount.getId());
                AppInfo appInfo = appInfoService.getAppInfoById(smsDomain.getAppId());
                try {
                    moInfo.setAddSerial(smsAccount.getAddSerial());
                    if (appInfo != null && !StringUtils.isEmpty(appInfo.getMoMsgUrl())) {
                        restUtil.post(appInfo.getMoMsgUrl(), JSONObject.parseObject(JSONObject.toJSONString(moInfo)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (smsAccount != null && smsAccount.getIsOutCall() == 0) {
                //本系统调用
                moInfoRepository.saveAndFlush(moInfo);
            }
            message.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }
}
